﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.DTO;
using LuShanTea.IService;

using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class CommentController : Controller
    {
        public ICommentService commentSer { get; set; }
        public IGoodService goodSer { get; set; }
        // 页面一加载时候获取的数据
        [CheckPermission("Comment.List")]
        [HttpGet]
        public ActionResult List()
        {
            var model = commentSer.GetPageData(1);
            ViewBag.totalCount = commentSer.GetAll().Count();
            return View(model);
        }

        // 分页获取数据
        [CheckPermission("Comment.List")]
        [HttpPost]
        public ActionResult List(int index=1)
        {
            var model = commentSer.GetPageData(index);
            ViewBag.totalCount = commentSer.GetAll().Count();
            return Json(model);
        }


        // 获取指定商品的评论
        [CheckPermission("Comment.List")]
        [HttpGet]
        public ActionResult LookGoodCommentList(long goodId)
        {
            CommentDTO[] model = commentSer.GetPageDataComment(goodId,1);
            
            if(model.Count()<=0) // 说明该商品还没有评论
            {
               
                return RedirectToAction("/List"); // 返回到评论列表
            }
            ViewBag.totalCount = commentSer.GetAll().Count();
            return View(model);
        }

        [CheckPermission("Comment.List")]
        [HttpPost]
        public ActionResult LookGoodCommentList(long goodId,int index=1)
        {
            CommentDTO[] model = commentSer.GetPageDataComment(goodId, index);
            ViewBag.totalCount = commentSer.GetAll().Count();
            return Json(model);
        }

        // 软删除
        [CheckPermission("Comment.Delete")]
        public ActionResult Delete(long commentId)
        {
            commentSer.MakeDelete(commentId);
            return Json(new AjaxResult { Status = "ok" });
        }
        // 批量删除
        [CheckPermission("Comment.Delete")]
        public ActionResult BatchDel(long[] commentIds)
        {
            foreach (var commentId in commentIds)
            {
                commentSer.MakeDelete(commentId);
            }
            return Json(new AjaxResult { Status = "ok" });
        }

        // 新增评论
        [CheckPermission("Comment.Add")]
        [HttpGet]
        public ActionResult AddNew(long goodId)
        {
            return View();
        }
        // 修改评论
        [CheckPermission("Comment.Edit")]
        [HttpGet]
        public ActionResult Edit(long commentId)
        {
           var model= commentSer.GetById(commentId);   
            return View(model);
        }
        [CheckPermission("Comment.Edit")]
        [HttpPost]
        public ActionResult Edit(CommentEditPostModel model)
        {
            CommentDTO dto = new CommentDTO();
            dto.CommentImgSrc = model.CommentImgSrc;
            dto.Content = model.Content;
            dto.CreateDateTime = DateTime.Now;
            dto.GoodId = model.GoodId;
            dto.OrderId = model.OrderId;
            dto.UserId = model.UserId;
            dto.Id = model.Id;
            dto.Level = model.Level;
            commentSer.UpdateComment(dto);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Comment.List")]
        [HttpPost]
        public ActionResult List2(int index = 1)
        {
            var model = commentSer.GetPageData(index);
            ViewBag.totalCount = commentSer.GetAll().Count();
            return Json(model);
            //return View(model);
        }
    }
}